﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CNRegex.Entity;
using CNRegex.Biz.BizAccess;
using CNRegex.Web.Base;
using Project.Common;
using CNRegex.Web.Utility;
using CNRegex.Enum;
using CNRegex.Biz;

namespace CNRegex.Web.Models
{
	public class ModelAccountHome:BaseModel
	{
		private BizRegexExpression bizRegexExpression;
		private BizRegexFavourite bizRegexFavorite;
		private BizRegexUserBlog bizRegexUserBlog;
		private BizRegexUser bizRegexUser;
		private Guid userID;
		
		public int? ExPageIndex { get; set; }
		public string SelectCategoryName { get; set; }
		public string Title
		{
			get
			{
				switch (ExpressionType)
				{
					case EnumExpressionType.myExpression:
						return "我的表达式";
					case EnumExpressionType.myArticle:
						return "我的文章";
					case EnumExpressionType.favoriteExpression:
						return "我的收藏";
					default:
						return "";
				}
			}
		}
		public EnumExpressionType ExpressionType { get; set; }
		public ModelAccountHome(string userName)
		{
			User = bizRegexUser.Get(userName);
			userID = User.ID;
		}

		private PaginateHelper exPaginate;
		public PaginateHelper ExPaginate
		{
			get
			{
				if (ExpressionType == EnumExpressionType.myArticle)
					myBlogs = MyBlogs;
				else
					myExpressions = MyExpressions;
				return exPaginate;
			}
		}

		private IList<RegexUserBlog> myBlogs;
		public IList<RegexUserBlog> MyBlogs
		{
			get
			{
				if (myBlogs != null)
					return myBlogs;
				int count = 0;
				if (string.IsNullOrEmpty(SelectCategoryName))
					myBlogs = bizRegexUserBlog.PaginateListBy(ProjectGlobal.PAGE_SIZE, ExPageIndex ?? 0, ref count, x => x.UserID == userID, x => new RegexExpression().CreateDate, Enum.EnumOrder.DESC);
				else
				{
					var selectCategory = UserBlogCategories.First(x => x.Name == SelectCategoryName);
					myBlogs = bizRegexUserBlog.PaginateListBy(ProjectGlobal.PAGE_SIZE, ExPageIndex ?? 0, ref count, x => x.UserID == userID && x.CatalogID == selectCategory.ID, x => new RegexExpression().CreateDate, Enum.EnumOrder.DESC);
				}
				exPaginate = new PaginateHelper(ProjectGlobal.PAGE_SIZE, count, ExPageIndex ?? 0, string.Format("/{0}?c={1}&t={2}&", User.Name, SelectCategoryName, (int)ExpressionType));
				exPaginate.ParaPageIndex = "p";
				return myBlogs;
			}
		}

		private IList<RegexExpression> myExpressions;
		public IList<RegexExpression> MyExpressions
		{
			get
			{
				if (myExpressions != null)
					return myExpressions;
				int count=0;

				if (ExpressionType == EnumExpressionType.myExpression)
				{
					if (string.IsNullOrEmpty(SelectCategoryName))
						myExpressions = bizRegexExpression.PaginateListBy(ProjectGlobal.PAGE_SIZE, ExPageIndex ?? 0, ref count, x => x.UserID == userID, x => new RegexExpression().CreateDate, Enum.EnumOrder.DESC);
					else
					{
						var selectCategory = RegexCategories.First(x => x.Name == SelectCategoryName);
						myExpressions = bizRegexExpression.PaginateListBy(ProjectGlobal.PAGE_SIZE, ExPageIndex ?? 0, ref count, x => x.UserID == userID && x.CategoryID == selectCategory.ID, x => new RegexExpression().CreateDate, Enum.EnumOrder.DESC);
					}
				}
				else
				{
					if (string.IsNullOrEmpty(SelectCategoryName))
						myExpressions = bizRegexFavorite.PaginateFavoriteExpression(ProjectGlobal.PAGE_SIZE, ExPageIndex ?? 0,null,User.ID,ref count);
					else
					{
						var selectCategory = RegexCategories.First(x => x.Name == SelectCategoryName);
						myExpressions = bizRegexFavorite.PaginateFavoriteExpression(ProjectGlobal.PAGE_SIZE, ExPageIndex ?? 0, selectCategory.ID, User.ID, ref count);
					}
				}
				exPaginate = new PaginateHelper(ProjectGlobal.PAGE_SIZE, count, ExPageIndex ?? 0, string.Format("/{0}?c={1}&t={2}&", User.Name, SelectCategoryName, (int)ExpressionType));
				exPaginate.ParaPageIndex = "p";
				return myExpressions;
			}
		}

		public RegexUser User
		{
			get;
			set;
		}

		private IList<RegexCategory> userExpressionCategories;
		public IList<RegexCategory> UserExpressionCategories
		{
			get
			{
				if (userExpressionCategories != null)
					return userExpressionCategories;
				IList<Guid> tmpList = bizRegexExpression.ListCategories(userID);
				userExpressionCategories = new List<RegexCategory>();
				for (int i = 0; i < tmpList.Count(); i++)
				{
					userExpressionCategories.Add(RegexCategories.First(x => x.ID == tmpList[i]));
				}
				return userExpressionCategories;
			}
		}

		private IList<RegexCategory> userFavoriteCategories;
		public IList<RegexCategory> UserFavoriteCategories
		{
			get
			{
				if (userFavoriteCategories != null)
					return userFavoriteCategories;
				IList<Guid> tmpList = bizRegexFavorite.ListFavoriteCategories(userID, EnumFavoriteType.Expression);
				userFavoriteCategories = new List<RegexCategory>();
				for (int i = 0; i < tmpList.Count(); i++)
				{
					userFavoriteCategories.Add(RegexCategories.First(x => x.ID == tmpList[i]));
				}
				return userFavoriteCategories;
			}
		}

		private IList<RegexUserBlogCatalog> userBlogCategories;
		public IList<RegexUserBlogCatalog> UserBlogCategories
		{
			get
			{
				if (userBlogCategories != null)
					return userBlogCategories;
				IList<Guid> tmpList = bizRegexUserBlog.ListUserBlogCataloies(userID);
				userBlogCategories = new List<RegexUserBlogCatalog>();
				for (int i = 0; i < tmpList.Count(); i++)
				{
					userBlogCategories.Add(RegexUserBlogCataloies.First(x => x.ID == tmpList[i]));
				}
				return userBlogCategories;
			}
		}

	}
}